import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
// 封装的请求方法
import request from '../../api/request'
// 登录
export const toLoginAction: any = createAsyncThunk('user/toLoginAction', async (payload) => {
    // 发送登录请求
    const result = await request.post('/api/auth/login', payload)
    return result

})


// 验证登录
export const isLoginAction = createAsyncThunk('user/isLoginAction', async () => {
    const result = await request.get('/api/auth/check_login')
    return result
})


// 更改头像
export const changedata:any = createAsyncThunk('user/changedata',async (payload) => {
    console.log('132456');
    
    const result = await request.post('/api/auth/changedata', payload)
    return result
})


interface userInfo {
    isAdmin: boolean;
    tel: number;
    username: string;
    avatar:string,
    __v: number;
    _id:string
}

const userSlice = createSlice({
    name: 'user',
    initialState: {
        userInfo: {} as userInfo | null,
        isLogin: !!localStorage.getItem('token')
    },
    reducers: {

    },
    extraReducers(build) {
        build.addCase(toLoginAction.fulfilled, (state, action) => {

            const code = action.payload.data?.code
            // console.log('action',action);

            if (code == 0) {
                const { payload: { data: { data: { userInfo, token } } } } = action;
                // 登录成功更新状态
                state.userInfo = userInfo
                state.isLogin = true
                localStorage.setItem('token', JSON.stringify(token))
            } else {
                // const { payload: { data: { message } } } = action;
                // console.log(message);
                // 登录失败更新状态
                state.userInfo = null
                state.isLogin = false
                // localStorage.removeItem('token')
            }
            // console.log('登录时的', localStorage.getItem('token'), state.isLogin);

            // console.log('state.userInfo',state.userInfo);


        })
        build.addCase(isLoginAction.fulfilled, (state, action) => {
            const code = action.payload.data?.code

            if (code == 0) {
                // token未过期
                const { payload: { data: { data } } } = action;
                const userInfo = data
                state.userInfo = userInfo
                // console.log('userInfo', userInfo);

            } else {
                // token过期
                // const { payload: { data: { message } } } = action;
                // useFun(message)
                // console.log(message);
                state.userInfo = null
                state.isLogin = false
                // localStorage.removeItem('token')

            }
            // console.log('验证的', localStorage.getItem('token'), state.isLogin);


            // console.log("action", state.userInfo);

        })
        build.addCase(changedata.fulfilled, (state, action) => {
            const code = action.payload.data?.code
            if (code == 0) {
                console.log('修改成功', action.payload.data?.changedata);
                state.userInfo = action.payload.data?.changedata
            }
        })
    }
})






export default userSlice.reducer;

